/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef RS_MODIFIERS_DRAW_H
#define RS_MODIFIERS_DRAW_H

#include <unordered_map>

#include "common/rs_optional_trace.h"
#include "recording/cmd_list_helper.h"
#include "recording/draw_cmd_list.h"
#include "transaction/rs_transaction_data.h"
#include "pipeline/rs_draw_cmd.h"
#include "platform/ohos/backend/native_buffer_utils_client.h"

#include "surface_buffer.h"

namespace OHOS {
namespace Rosen {
class RSModifiersDraw {
public:
    static std::unique_ptr<RSTransactionData>& ConvertTransaction(std::unique_ptr<RSTransactionData>& transactionData);

    static void RemoveSurfaceByNodeId(NodeId nodeId, bool postTask = false);

    static bool ResetSurfaceByNodeId(int32_t width, int32_t height, NodeId nodeId, bool postTask = false);

    static std::unique_ptr<Media::PixelMap> GetPixelMapByNodeId(NodeId nodeId, bool useDMA = false);

private:
    using SurfaceAndPixelMap = std::pair<std::shared_ptr<Drawing::Surface>, std::shared_ptr<Media::PixelMap>>;

    static sptr<SurfaceBuffer> DmaMemAlloc(
        int32_t width, int32_t height, const std::unique_ptr<Media::PixelMap>& pixelMap);

    static std::shared_ptr<Drawing::Surface> CreateSurfaceFromGpuContext(
        const std::unique_ptr<Media::PixelMap>& pixelMap, int32_t width, int32_t height);

    static std::shared_ptr<Drawing::Surface> CreateSurfaceFromCpuContext(
        const std::unique_ptr<Media::PixelMap>& pixelMap);

    static std::shared_ptr<Drawing::Surface> CreateSurface(std::unique_ptr<Media::PixelMap>& pixelMap,
        int32_t width, int32_t height);

    static void ConvertCmdListForCanvas(const std::shared_ptr<Drawing::DrawCmdList>& cmdList, NodeId nodeId);

    static void ConvertCmdList(const std::shared_ptr<Drawing::DrawCmdList>& cmdList);

    static void Playback(const std::shared_ptr<Drawing::Surface>& surface,
        const std::shared_ptr<Drawing::DrawCmdList>& cmdList, bool isCanvasType);

    static void InvalidateSurfaceCache(const std::shared_ptr<Media::PixelMap>& pixelMap);

    static void AddPixelMapDrawOp(const std::shared_ptr<Drawing::DrawCmdList>& cmdList,
        const std::shared_ptr<Media::PixelMap>& pixelMap, int32_t width, int32_t height);

    static bool TargetCommad(const std::unique_ptr<RSCommand>& cmd);

    static std::unique_ptr<Media::PixelMap> CreatePixelMap(int32_t width, int32_t height);

    static SurfaceAndPixelMap GetSurfaceAndPixelMapByNodeId(NodeId nodeId);

    static std::unordered_map<NodeId, SurfaceAndPixelMap> surfaceMap_;

    static std::mutex surfaceMutex_;
};
} // namespace Rosen
} // namespace OHOS
#endif // RS_MODIFIERS_DRAW_H